package com.amap.demo.view;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.amap.api.maps.AMap;
import com.amap.api.maps.AMapOptions;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.LatLng;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 地图控制<br />
 * 包括，地图控件的显示隐藏。点击事件，拖动事件，长按事件, 触摸事件<br/>
 * 缩放手势
 * 平移手势
 * 旋转手势
 * 倾斜手势
 * 截屏
 *
 * @author ys
 */
public class MapControllActivity extends AppCompatActivity implements View.OnClickListener, AMap.OnMapClickListener,
        AMap.OnMapLongClickListener, AMap.OnMapLoadedListener, AMap.OnMapScreenShotListener, AMap.OnMapTouchListener {

    @Bind(R.id.controll_mapview)
    MapView mapView;
    @Bind(R.id.zoom_in_btn)
    Button zoomInBtn;
    @Bind(R.id.zoom_out_btn)
    Button zoomOutBtn;
    @Bind(R.id.rotate_btn)
    Button rotateBtn;
    @Bind(R.id.tilt_btn)
    Button tiltBtn;
    @Bind(R.id.screen_shot_btn)
    Button screenShotBtn;

    private AMap aMap;

    private UiSettings uiSettings;

    private float angle = 0f;

    private float tilt = 0f;

    private CameraUpdate cameraUpdate = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_controll);
        ButterKnife.bind(this);

        mapView.onCreate(savedInstanceState);
        init();
    }

    /**
     * 初始化操作
     */
    private void init() {
        if (null == aMap) {
            aMap = mapView.getMap();
        }
        uiSettings = aMap.getUiSettings();

        //
        uiSettings.setZoomControlsEnabled(false);//不显示缩放控件
        uiSettings.setLogoPosition(AMapOptions.LOGO_POSITION_BOTTOM_RIGHT);//设置logo的位置
        uiSettings.setCompassEnabled(true);//启用指南针
        uiSettings.setScaleControlsEnabled(true);//启用比例尺


        aMap.getMyLocation();
        aMap.setOnMapClickListener(this);
        aMap.setOnMapLongClickListener(this);
        aMap.setOnMapLoadedListener(this);
        aMap.setOnMapTouchListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        mapView.onSaveInstanceState(outState);
    }

    @OnClick({R.id.zoom_in_btn, R.id.zoom_out_btn, R.id.rotate_btn, R.id.tilt_btn, R.id.screen_shot_btn})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.zoom_out_btn://缩小
                cameraUpdate = CameraUpdateFactory.zoomOut();
                aMap.animateCamera(cameraUpdate);
                Toast.makeText(MapControllActivity.this, "当前缩放级别是：" + aMap.getScalePerPixel(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.zoom_in_btn://放大
                cameraUpdate = CameraUpdateFactory.zoomIn();
                aMap.animateCamera(cameraUpdate);
                break;
            case R.id.rotate_btn://旋转
                cameraUpdate = CameraUpdateFactory.changeBearing(angle += 30);
                aMap.animateCamera(cameraUpdate);
                break;
            case R.id.tilt_btn://倾斜
                if (tilt < 45f) {
                    cameraUpdate = CameraUpdateFactory.changeTilt(tilt += 15);
                    aMap.animateCamera(cameraUpdate);
                } else {
                    Toast.makeText(MapControllActivity.this, "地图倾斜度范围(0, 45)", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.screen_shot_btn:
                aMap.getMapScreenShot(MapControllActivity.this);
                break;
        }
    }


    @Override
    public void onMapClick(LatLng latLng) {
        Toast.makeText(MapControllActivity.this, "地图单击 \n" +
                "latlng --(" + latLng.longitude + "," + latLng.latitude + ")", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onMapLongClick(LatLng latLng) {
        Toast.makeText(MapControllActivity.this, "地图长按 \n" +
                "latlng --(" + latLng.longitude + "," + latLng.latitude + ")", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onMapLoaded() {
        Toast.makeText(MapControllActivity.this, "地图载入成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onMapScreenShot(Bitmap bitmap) {
        if (null == bitmap){
            return ;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        try {
            String fileName = Environment.getExternalStorageDirectory() + "test_" + sdf.format(new Date()) + ".png";
            FileOutputStream fos = new FileOutputStream(fileName);
            boolean bool = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
            if (bool){
                Toast.makeText(MapControllActivity.this, "截屏保存成功 \n" + fileName, Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(MapControllActivity.this, "截屏保存失败", Toast.LENGTH_SHORT).show();
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onTouch(MotionEvent motionEvent) {
        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Toast.makeText(MapControllActivity.this, "按下", Toast.LENGTH_SHORT).show();
                break;
            case MotionEvent.ACTION_MOVE:
                Toast.makeText(MapControllActivity.this, "移动", Toast.LENGTH_SHORT).show();
                break;
            case MotionEvent.ACTION_UP:
                Toast.makeText(MapControllActivity.this, "抬起", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

